<template>
    <Form ref="formValidate" class="exform" :model="formValidate" :label-width="80" :rules="ruleValidate">
		<FormItem label="民族" prop="nation"  style="width:50%">
			<Select v-model="formValidate.nation" style="width:80%" transfer>
				<Option v-for="item in formItem.nation" :value="item.dictionary_id" :key="item.value">
					{{item.dictionary_name.zh?item.dictionary_name.zh:item.dictionary_name.mn}}
				</Option>
			</Select>
		</FormItem>
		<FormItem label="姓名" prop="name.zh"  style="width:50%">
			<Input v-model="formValidate.name.zh" placeholder="姓名" style="width:80%">
			</Input>
		</FormItem>
		<FormItem label="性别" prop="gender" style="width:50%">
			<Select v-model="formValidate.gender" style="width:80%" transfer>
				<Option v-for="item in formItem.gender" :value="item.dictionary_id" :key="item.value">
					{{item.dictionary_name.zh?item.dictionary_name.zh:item.dictionary_name.mn}}
				</Option>
			</Select>
		</FormItem>
		<FormItem label="身份证号" prop="idcard"  style="width:50%">
			<Input v-model="formValidate.idcard" placeholder="身份证号码" style="width:80%">
			</Input>
		</FormItem>
		<FormItem label="证照号码" prop="passportnumber" style="width:50%">
			<Input v-model="formValidate.passportnumber" placeholder="证照号码" style="width:80%">
			</Input>
		</FormItem>
		<FormItem label="出国(境)时间" prop="time" style="width:50%">			
			<DatePicker placement="left" v-model="formValidate.time" type="date"  placeholder="请选择出国(境)时间" style="width: 200px" transfer></DatePicker>
		</FormItem>
		<FormItem label="出国(境)地点" prop="nationality" style="width:50%" >
			<Select v-model="formValidate.nationality"  style="width:80%" transfer>
				<Option v-for="item in formItem.nationality" :value="item.dictionary_id" :key="item.dictionary_id"> {{item.dictionary_name.zh?item.dictionary_name.zh:item.dictionary_name.mn}}</Option>
			</Select>
		</FormItem>
		<FormItem label="回国(境)时间" prop="entime" style="width:50%">
			<DatePicker placement="left" v-model="formValidate.entime" type="date"  placeholder="请选择回国(境)时间" style="width: 200px" transfer></DatePicker>
		</FormItem>
		<FormItem label="费用来源" prop="sourceofcost.zh" style="width:50%">
			<Input v-model="formValidate.sourceofcost.zh" placeholder="费用来源" style="width:80%">
			</Input>
		</FormItem>
		<FormItem label="海外关系" prop="sourceofcostwa.zh" style="width:50%">
			<Input v-model="formValidate.sourceofcostwa.zh" placeholder="海外关系" style="width:80%">
			</Input>
		</FormItem>
		<FormItem label="申请事由" prop="reasonsabroad.zh">
			<Input v-model="formValidate.reasonsabroad.zh" type="textarea" :autosize="{minRows: 2,maxRows: 4}" placeholder="申请事由"></Input>
		</FormItem>
		<div style="width:90%;">
			<FormItem style="width:100%;">
			<Button type="primary" @click="handleSubmit('formValidate')">保存</Button>
			</FormItem>
		</div>
		<div style="clear:both"></div>
		<Spin size="large" fix v-if="spinShow"></Spin>
    </Form>
</template>

<script>

  export default {

    props: {
        teacher_id: {
            type: [Number,String],
            default: null
        },
        infoId: {
            type: [Boolean,String,Number],
            default: false
        }
    },
    data () {
      	return {
            infoUrl:`api/teacher/v1/pmbsqualification/info/${this.infoId}`,//请求单条数据
            saveUrl:'',//保存接口
            saveType:null,//保存方式
            spinShow:false,//加载动画

			infoOld:null,
			formItem:[], 
			
			formValidate: {
				teacher_id:this.teacher_id,
				passportnumber:'',    //证照号码	
				nationality:'',    //出境地点	
				idcard:'',    //身份证号	
				nation:'',    //民族	
				gender:'',    //性别	
          		companyopinionid:1,//所在单位审核人id
				time:'',     //出境时间
				entime:'',     //回国时间
				sourceofcost:{
					zh:'',
					mn:'',
					en:'',
				},    //费用来源
				sourceofcostwa:{
					zh:'',
					mn:'',
					en:'',
				},    //海外关系
				name:{
					zh:'',
					mn:'',
					en:'',
				},    //姓名	
				reasonsabroad:{
					zh:'',
					mn:'',
					en:'',
				},    //出国申请理由
			},
			ruleValidate: {
				// 民族
				nation: [
					{ type:'number',required: true, message: '请选择民族', trigger: 'change' }
				],
				// 性别
				gender: [
					{ type:'number',required: true, message: '请选择性别', trigger: 'change' }
				],
				// 出境地点
				nationality: [
					{ type:'number',required: true, message: '请选择出境地点', trigger: 'change' }
				],
				// 出境时间
				time: [
					{ required: true, type: 'date', message: '请选择时间', trigger: 'blur' }
				],
				// 回国时间
				entime: [
					{ required: true, type: 'date', message: '请选择时间', trigger: 'blur' }
				],
				// 证照号码
				passportnumber: [
					{ required: true, message: '请填写证照号码', trigger: 'blur' }
				],
				// 身份证号
				idcard: [
					{ required: true, message: '请填写身份证号', trigger: 'blur' }
				],
				//费用来源
				'sourceofcost.zh': [
					{ required: true, message: '请填写费用来源', trigger: 'blur' }
				],
				//海外关系
				'sourceofcostwa.zh': [
					{ required: true, message: '请填写海外关系', trigger: 'blur' }
				],
				//姓名
				'name.zh': [
					{ required: true, message: '请填写姓名', trigger: 'blur' }
				],
				//出国申请理由
				'reasonsabroad.zh': [
					{ required: true, message: '请填写出国申请理由', trigger: 'blur' }
				],
			},  
      	}
    },
    methods: {
     
        /************************************************************
         * 
         * 单条数据请求
         * 
         ************************************************************/
        infoData(){
            this.spinShow=true//输入框禁用  
            this.$http.get(this.infoUrl).then(val=>{
              
                this.infoOld = JSON.parse(JSON.stringify(val.data))
                this.formValidate.nation = parseInt(this.infoOld.nation)//用于默认选的 民族
                this.formValidate.gender = parseInt(this.infoOld.gender)//用于默认选的 性别
                this.formValidate.nationality = parseInt(this.infoOld.nationality)//用于默认选的 出境地点
                // this.formValidate.companyopinion = parseInt(this.infoOld.companyopinion)//用于默认选的 审核
                
                this.formValidate.passportnumber = this.infoOld.passportnumber// 证照号码
                this.formValidate.idcard = this.infoOld.idcard// 身份证号
                this.formValidate.time = this.infoOld.time// 出境时间
                this.formValidate.entime = this.infoOld.entime// 回国时间

                this.formValidate.sourceofcost = this.infoOld.sourceofcost// 费用来源
                this.formValidate.sourceofcostwa = this.infoOld.sourceofcostwa// 海外关系
                this.formValidate.name = this.infoOld.name// 姓名
                this.formValidate.reasonsabroad = this.infoOld.reasonsabroad// 出国申请理由
                this.formValidate.school_id=this.infoOld.school_id;//学校
                this.formValidate.teacher_id=this.infoOld.teacher_id;//教师 
                this.spinShow=false//输入框禁用   

            }).catch(err => {
				if(err.code && err.code==422) {
					this.$Message.error(err.message);
				}
                this.spinShow=false//输入框禁用  
			});
		},
		
        /************************************************************
         * 
         * 提交时
         * 
         ************************************************************/
		handleSubmit (name) {
			this.$refs[name].validate((valid) => {
                if (valid) {       
                    this.spinShow=true//输入框禁用   
                    this.saveType(this.saveUrl,this.formValidate).then(val=>{
                        this.$emit('showParent',false)
                        this.$emit('backList',true)
                        this.$Message.success(val.message);
                        this.spinShow=false
                    }).catch(err => {
                        if (err.code && err.code == 422) {
                            this.$Message.error(err.message);
                        }
                        this.spinShow=false
                    })  
                } else {
                    this.$Message.error('表单验证失败!');
                }
			})
		},
    },
    created() {
        /************************************************************
         * 
         * 查询资格类型
         * 
         ************************************************************/
		this.$http.get('api/dictionary/v1/dictionary/pidquerys?gender&nation&nationality')
		.then(val=>{
			this.formItem = val.data
			this.spinShow=false//输入框禁用 
		})
		.catch(err => {
			if(err.code && err.code==422) {
				this.$Message.error(err.message);
			}
			this.spinShow=false//输入框禁用 
		});

        if(this.infoId==false){

			/************************************************************
			 * 
			 * 当前用户信息
			 * 
			 ************************************************************/
			this.$http.get('api/teacher/v1/teacher/find/'+this.teacher_id)
			.then(val=>{
				let res =val.data
				this.formValidate.idcard=res.idcard_org
				this.formValidate.name=res.teacher_name
				this.formValidate.gender=parseInt(res.gender)
				this.formValidate.nation=parseInt(res.nation)
				this.spinShow=false//输入框禁用 
			})
			.catch(err => {
				if(err.code && err.code==422) {
					this.$Message.error(err.message);
				}
				this.spinShow=false//输入框禁用 
			});
            // add
            this.UpImgShow=true
            this.saveUrl='api/teacher/v1/pmbsqualification/add'
            this.saveType=this.$http.post

        }else{
            // edit
            this.infoData();
            this.saveUrl=`api/teacher/v1/pmbsqualification/edit/${this.infoId}`
            this.saveType=this.$http.post
        }
    },
}
</script>
<style lang="less">
@import'./../public.less';
</style>
